﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Model;
using VAdvantage.Utility;
using System.Threading;
using VAdvantage.Model;
using VAdvantage.Classes;
using System.ComponentModel;

namespace VAdvantage.Apps
{
    public partial class AddChartToTab : CFrame
    {
        public AddChartToTab()
        {
            InitializeComponent();
            Utility.Envs.RegisterWindowForm(this);

        }

        int D_Chart_ID = 0;
        public AddChartToTab(int chartid, List<KeyNamePair> tabList)
            : base(Msg.GetMsg("EditChartOnTab"))
        {
            D_Chart_ID = chartid;
            InitializeComponent();
            Utility.Envs.RegisterWindowForm(this);

            SetBusy(true, "Loading");

            LoadText(tabList);
            LoadCharts();
        }

        ViennaRoleCenter rolecenter = null;

        public AddChartToTab(ViennaRoleCenter _rolecenter)
            : base(Msg.GetMsg("AddChartToTab"))
        {
            rolecenter = _rolecenter;
            InitializeComponent();
            Utility.Envs.RegisterWindowForm(this);

            SetBusy(true, "Loading");

            LoadText(rolecenter.ListTab);
            LoadCharts();
        }


        void LoadText(List<KeyNamePair> tabList)
        {
            lblCharts.Content = Msg.GetMsg("Charts");
            lblTabs.Content = Msg.GetMsg("Tab");
            lblRows.Content = Msg.GetMsg("Rowspan");
            lblCols.Content = Msg.GetMsg("Colspan");
            lblSeq.Content = Msg.GetMsg("SeqNo");

            int index = 0;
            for (int i = 0; i <= tabList.Count - 1; i++)
            {
                vTabs.Items.Add(tabList[i]);
                if (tabList[i].GetKey() == rolecenter.RC_Tab_ID)
                    index = i;
            }

            if (rolecenter.EditMode)
            {
                vTabs.SelectedIndex = index;
                txtSeq.Text = rolecenter.SeqNo.ToString();
                txtCols.Text = rolecenter.Colspan.ToString();
                txtRows.Text = rolecenter.Rowspan.ToString();
            }
        }

        void LoadCharts()
        {
            ThreadPool.QueueUserWorkItem(delegate
            {
                MChart[] charts = MChart.GetCharts();
                int index = 0;
                for (int c = 0; c <= charts.Count() - 1; c++)
                {
                    KeyNamePair chart = new KeyNamePair(charts[c].GetD_Chart_ID(), charts[c].GetName());
                    Dispatcher.BeginInvoke(() => vCharts.Items.Add(chart));
                    Dispatcher.BeginInvoke(() => SetBusy(false));
                    if (charts[c].GetD_Chart_ID() == rolecenter.D_Chart_ID)
                        index = c;
                }

                if (rolecenter.D_Chart_ID > 0)
                {
                    Dispatcher.BeginInvoke(() => vCharts.SelectedIndex = index);
                    Dispatcher.BeginInvoke(() => vCharts.SetReadOnly(true));
                }
                else
                    Dispatcher.BeginInvoke(() => SetBusy(false));
            });
        }


        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int TabPanels_ID = 0;
                int ChartPanel_ID = 0;
                if (rolecenter.EditMode)
                {
                    TabPanels_ID = rolecenter.TabPanel_ID;
                    ChartPanel_ID = rolecenter.ChartPane_ID;
                }

                SetBusy(true, "Saving");
                //txtBusy.Text = Msg.GetMsg("Saving");
                //busyWindow.Visibility = System.Windows.Visibility.Visible;

                KeyNamePair chartSelected = (KeyNamePair)vCharts.SelectedItem;
                KeyNamePair frameSelected = (KeyNamePair)vTabs.SelectedItem;
                int rowspan = int.Parse(txtRows.Text);
                int colspan = int.Parse(txtCols.Text);
                int seqno = int.Parse(txtSeq.Text);

                bool save = false;
                BackgroundWorker bgw = new BackgroundWorker();
                bgw.DoWork += (ov, ev) =>
                {
                    X_RC_ChartPane saveChart = new X_RC_ChartPane(Envs.GetCtx(), ChartPanel_ID, null);
                    saveChart.SetRowspan(rowspan);
                    saveChart.SetColspan(colspan);
                    saveChart.SetD_Chart_ID(chartSelected.GetKey());

                    if (saveChart.Save())
                    {
                        X_RC_TabPanels tabPanels = new X_RC_TabPanels(Envs.GetCtx(), TabPanels_ID, null);
                        tabPanels.SetRC_RoleCenterTab_ID(frameSelected.GetKey());
                        tabPanels.SetRecord_ID(saveChart.Get_ID());
                        tabPanels.SetRoleCenterPanels("2");
                        tabPanels.SetSeqNo(seqno);
                        tabPanels.SetColspan(colspan);
                        tabPanels.SetRowspan(rowspan);

                        save = tabPanels.Save();
                    }
                };

                bgw.RunWorkerCompleted += (ov, ev) =>
                {
                    if (save)
                    {
                        ShowMessage.Info(Msg.GetMsg("RecordSaved"), false, "", "");
                        if (!rolecenter.EditMode)
                        {
                            vTabs.SelectedIndex = -1;
                            vCharts.SelectedIndex = -1;
                            txtCols.Text = "";
                            txtRows.Text = "";
                            txtSeq.Text = "";
                        }
                    }
                    else
                        ShowMessage.Info(Msg.GetMsg("RecordNotSaved"), false, "", "");

                    SetBusy(false);
                };

                bgw.RunWorkerAsync();
            }
            catch
            {
                ShowMessage.Info(Msg.GetMsg("DataNotCorrect"), false, "", "");
                SetBusy(false);
                return;
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void SetBusy(bool b)
        {
            SetBusy(false, "");
        }

        void SetBusy(bool b, String t)
        {
            if (b)
            {
                busyWindow.Visibility = System.Windows.Visibility.Visible;
                txtBusy.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                busyWindow.Visibility = System.Windows.Visibility.Collapsed;
            }
            txtBusy.Text = t;

        }
    }
}
